package com.tvunetworks.center.user.service.impl;

import com.tvunetworks.center.user.service.GridServerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program:
 * @description:
 * @author HenryChen
 * @create: 11/28/2019 2:24 PM
 **/
@Service
@Slf4j
public class GridServerServiceImpl implements GridServerService {


    @Value("${gridServer.url}")
    private String gridServer;

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public List<Map<String, String>> getBindXByRids(String rIds, String condition) throws Exception {

        ResponseEntity<Map> forEntity = restTemplate.getForEntity(gridServer + "/getGridSourceByRidForGM.action?rid={rIds}&condition={condition}", Map.class, rIds, condition);

        if (forEntity.getStatusCode() != HttpStatus.OK) {
            return new ArrayList<>(0);
        }

        Map<String, Object> body = forEntity.getBody();
        if(body!=null){
            Object resultObj = body.get("result");

            if (resultObj == null) {
                return new ArrayList<>();
            }

            List<Map<String, String>> list = (List<Map<String, String>>) resultObj;

            if (log.isInfoEnabled()) {
                log.info("Grid Server getGridSourceByRidForGM result {}", list);
            }

            return list;
        }
            return null;
    }
}
